package org.example.cet.demos.controller;


import org.example.cet.demos.converter.converter;
import org.example.cet.demos.domain.PageBean;
import org.example.cet.demos.domain.Result;
import org.example.cet.demos.domain.query.PapersQuery;
import org.example.cet.demos.service.impl.PapersServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hq
 * @since 2024-12-18
 */
@RestController
@ResponseBody
@RequestMapping("/papers")
public class PapersController {
    @Autowired
    private PapersServiceImpl papersService;
    @Autowired
    private converter converter;

//    @PostMapping("/add")
//    public Result add(@Valid @RequestBody PapersForm papersForm) {
//        Papers papers=converter.ToPapers(papersForm);
//        papersService.save(papers);
//        return Result.success();
//    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        papersService.delete(id);
        return Result.success();
    }

//    @PutMapping("/update/{id}")
//    public Result update(@PathVariable Long id,@Valid @RequestBody PapersForm papersForm) {
//        Papers papers=converter.ToPapers(papersForm);
//        papers.setId(id);
//        papersService.updateById(papers);
//        return Result.success();
//    }

    @GetMapping("/getPage")
    public Result getPage(PapersQuery papersQuery) {
        PageBean pb=papersService.getPage(papersQuery);
        return Result.success(pb);
    }

//    @GetMapping("/getPaper/{id}")
//    public Result getPaper(@PathVariable Long id) {
//        List<PaperDto> paper=papersService.getPaper(id);
//        return Result.success(paper);
//    }
//
//    @GetMapping("/getList")
//    public Result getList() {
//        return Result.success(papersService.getList());
//    }



    @PostMapping("/upload")
    public Result handleFileUpload(@RequestParam("file") MultipartFile file,
                                   @RequestParam("title") String title,
                                   @RequestParam("type") String paperType,
                                   @RequestParam("year") String year,
                                   @RequestParam("answer") String answer) {
        String result = papersService.handleFileUpload(file, title, paperType, year, answer);
//        return ResponseEntity.ok(result);
        return Result.success(result);
    }

//    @GetMapping("/questions/type/{type}")
//    public ResponseEntity<List<Questions>> getQuestionsByType(@PathVariable int type) {
//        List<Questions> questions = papersService.getQuestionsByType(type);
//        return ResponseEntity.ok(questions);
//    }
//
//    @GetMapping("/questions/paper/{paperId}")
//    public ResponseEntity<List<Questions>> getQuestionsByPaperId(@PathVariable int paperId) {
//        List<Questions> questions = papersService.getQuestionsByPaperId(paperId);
//        return ResponseEntity.ok(questions);
//    }

    @GetMapping("/getPaperList")
    public Result getPaperList() {
        return Result.success(papersService.getPaperList());
    }

    @GetMapping("/getPaper/{id}")
    public Result getPaper(@PathVariable Long id) {
        List<Map<String,Object>> paper = papersService.getPaper(id);
        return Result.success(paper);
    }


}
